package at.codebase.web.templates;

import static at.codebase.web.util.i18n.I18NFactory.i18n;
import at.codebase.db.entities.EUser;
import at.codebase.web.util.LocalRequest.RequestInformation;
import at.codebase.web.util.servlet.SecureServlet.UserLevel;
import at.codebase.web.util.template.Template;
import at.codebase.web.util.template.TemplateEngine;

/**
 * 
 * @author Lukas Pfeifhofer <lukas.pfeifhofer@devlabs.pro>
 *
 */
public class UserProfileTemplate extends Template {

	private static String TEMPLATE_URL = "user/site_profile_container.html";

	public UserProfileTemplate(RequestInformation requestInfo, UserLevel level, EUser user) {
		super(TemplateEngine.getTemplateContent(TEMPLATE_URL));
		init(requestInfo, level, user);
	}

	private void init(RequestInformation requestInfo, UserLevel level, EUser user) {

		setText("userId", String.valueOf(user.getId()));

		/*
		 * Global
		 */
		if(!user.isActive()){
			setHTML("dectivatedFrame", "<div class=\"userProfileDectivatedFrame\">This Account is not active!</div>");
		}
		
		if(user.getImage() == null){
			setText("imageUrl", "/images/no_image_available.png");	
		}else{
			setText("imageUrl", "/service/image?id="+user.getImage().getId()+"&w=140&h=140");
		}
		
		
		
		/*
		 * Admin Menu
		 */
		if(level.equals(UserLevel.ADMIN)){
			Template adminMenu = TemplateEngine.getTemplate("user/site_profile_menu_admin.html");
			adminMenu.setText("userId", String.valueOf(user.getId()));
			if(!user.isActive()){
				adminMenu.setText("activateToggle", "activate");
				adminMenu.setText("activateToggleText", i18n(requestInfo.getLocale()).string("userProfile_menu_activate"));
			}else{
				adminMenu.setText("activateToggle", "deactivate");
				adminMenu.setText("activateToggleText", i18n(requestInfo.getLocale()).string("userProfile_menu_deactivate"));
			}
			setTemplate("userAdminMenu", adminMenu);
		}
		if(requestInfo.isUserLoggedIn()){
			/*
			 * User Menu
			 */
			if(user.getId().equals(requestInfo.getUser().getId())){
				Template userMenu = TemplateEngine.getTemplate("user/site_profile_menu_user.html");
				userMenu.setText("userId", String.valueOf(user.getId()));
				if(!user.isActive()){
					userMenu.setText("activateToggle", "activate");
					userMenu.setText("activateToggleText", i18n(requestInfo.getLocale()).string("userProfile_menu_activate"));
				}else{
					userMenu.setText("activateToggle", "deactivate");
					userMenu.setText("activateToggleText", i18n(requestInfo.getLocale()).string("userProfile_menu_deactivate"));
				}
				setTemplate("userUserMenu", userMenu);
			}else{
			/*
			 * Stranger Menu
			 */
				Template strangerMenu = TemplateEngine.getTemplate("user/site_profile_menu_stranger.html");
				strangerMenu.setText("userId", String.valueOf(user.getId()));
				setTemplate("userStrangerMenu", strangerMenu);
			}
		}else{
			//No Anonymous Menu
		}

	}

	public void setContent(Template template) {
		setTemplate("userContent", template);
	}
	
}
